<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="./js/M_select/M_select.css">
    <link href="css/buttonStyle.css" rel='stylesheet' type='text/css' />
    <script src="js/jquery-3.3.1.js"></script>
    <script src="layer/layer.js"></script>
    <script src="js/M_select/M_select.js"></script>
    <script src="json/select_json.json"></script>
</head>
<body>
<div id="addressform" style="width:550px; height:450px;padding:10px;border:1px solid #EEEEEE;">
    <h2>编辑地址</h2>
    <div id="addAddress">

    </div>
    <!--<div style="float: left;padding: 200px 0px 0px 60px;width: 200px" id="action4Product">
        <a href="#" onclick="placeOrder()" style="padding:20px 80px 20px 80px" class="hvr-bounce-to-top button">购买</a>
    </div>-->
</div>
</body>
</html>

<script>
    //传来的参数值
    var datavalue;
    var userid;

    //调用检测自己的登录状态
    proveMe();

    //检测自己的登录状态
    function proveMe() {
        $.ajax({
            type:'post',
            url:'/user/proveMe',
            data:'',
            cache:false,
            dataType:'json',
            success:function(data){
                //登陆成功，则...
                if(data.code==200){
                    var user = data.data.user;
                    userid = user.userid;
                    var account = user.account;
                    var username = user.username;
                    var email = user.email;
                    //展示地址表单
                    showFrom();
                    //获得商品信息
                    //getProductInfo();
                }
            }
        });
    }

    //显示表单
    function showFrom() {

        var receiver = '';
        var phonenum = '';
        var addressvalue = '';

        var url = window.location.search; /* 获取属性（“?”后面的分段） */
        var data = url.split("?"); //得到所有的参数
        datavalue = data[1].split("=");
        //如果传了参数，则为修改，那么需得到地址信息
        if(datavalue[1] != '-1'){
            $.ajax({
                async: false,
                type: 'get',
                url: '/address/selectById',
                data: data[1],
                cache: false,
                dataType: 'json',
                success: function (data) {

                    receiver = data.receiver;
                    phonenum = data.phonenum;
                    addressvalue = data.addressvalue;

                }
            })
        }

        $("#addAddress").empty();
        $("#addAddress").append("<form id='myform'>"+
            "<ul id='orderList' style='list-style:none;line-height: 60px;'>"+
        "<li style=' font-size: 20px;color:#262626' ><span>收 件 人 </span><input style='height: 30px;width: 180px' id='receiver' name='receiver' type='text' value='"+receiver+"'></li>"+
        "<li style=' font-size: 20px;color:#262626' ><span>联系方式 </span><input style='height: 30px;width: 180px' id='phonenum' name='phonenum' type='text' value='"+phonenum+"'></li>"+
        "<li style=' font-size: 20px;color:#262626' ><span>详细地址</span></li>"+
        "<li><textarea id='addressvalue' name='addressvalue' style='resize:none'  rows='5' cols='60' >"+addressvalue+"</textarea></li>"+
        "<li><a href='#' onclick='action4Address()' style='float:right;margin-right:80px;padding:0px 60px 0px 60px' class='hvr-bounce-to-top button'>提交</a></li>"+
        "</ul>"+
        "</form>");

    }

    //新增或修改地址信息
    function action4Address() {
        //修改地址信息
        if(datavalue[1] != '-1'){
            $.ajax({
                type:'post',
                url:'/address/updateAddress',
                data:$("#myform").serialize()+"&addressid="+datavalue[1],
                cache:false,
                dataType:'json',
                success:function(data) {
                    if(data.status == 200){
                        layer.msg(data.data);
                        $("#addressform").empty();
                        $("#addressform").append("<h2>修改成功,请刷新查看</h2>");
                    }else{
                        layer.msg(data.data);
                    }

                }
            });
        }else{
            //新增地址信息
            $.ajax({
                type:'post',
                url:'/address/addAddress',
                data:$("#myform").serialize()+"&userid="+userid,
                cache:false,
                dataType:'json',
                success:function(data) {
                    if(data.status == 200){
                        layer.msg(data.data);
                        $("#addressform").empty();
                        $("#addressform").append("<h2>添加成功,请刷新查看</h2>");
                    }else{
                        layer.msg(data.data);
                    }

                }
            });
        }

    }

</script>
